package com.jingdianjichi.auth.controller;

import com.jingdianjichi.auth.common.entity.Result;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.convert.AuthPermissionDTOConverter;
import com.jingdianjichi.auth.domin.entity.AuthPermissionBO;
import com.jingdianjichi.auth.domin.service.AuthPermissionDomainService;
import com.jingdianjichi.auth.dto.AuthPermissionDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 角色 controller
 * @return:
 * @Author:  sky
 * @date:  2024/11/11 15:57
 */
@RestController
@RequestMapping("/permission/")
@Slf4j
public class PermissionController {

    @Autowired
    private AuthPermissionDomainService authPermissionDomainService;


    /**
     * @Description: 新增权限
     * @return:
     * @Author: sky
     * @date: 2024/11/11 16:09
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            // 判断是否启动info级别的日志，如果启动了再执行json转换，减少性能开销
            if (log.isInfoEnabled()) {
                log.info("log-info:{}", JSON.toJSONString(authPermissionDTO));
            }

            // 字符串需要同时检测， null 和 空串 两种情况，断言匹配false抛出异常
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()), "权限名称不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "权限父id不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            // alt + enter 快速生成返回变量
            return Result.success(authPermissionDomainService.add(authPermissionBO));
        } catch (Exception e) {
            log.error("log-error:{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
    }


    /**
     * @Description: 更新权限
     * @return:
     * @Author: sky
     * @date: 2024/11/11 16:17
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            // 判断是否启动info级别的日志，如果启动了再执行json转换，减少性能开销
            if (log.isInfoEnabled()) {
                log.info("log-info:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            // alt + enter 快速生成返回变量
            return Result.success(authPermissionDomainService.update(authPermissionBO));
        } catch (Exception e) {
            log.error("log-error:{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
    }


    /**
     * @Description: 删除权限
     * @return:
     * @Author: sky
     * @date: 2024/11/11 16:17
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            // 判断是否启动info级别的日志，如果启动了再执行json转换，减少性能开销
            if (log.isInfoEnabled()) {
                log.info("log-info:{}", JSON.toJSONString(authPermissionDTO));
            }

            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            // alt + enter 快速生成返回变量
            return Result.success(authPermissionDomainService.delete(authPermissionBO));
        } catch (Exception e) {
            log.error("log-error:{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    /**
     * @Description: 查询用户权限
     * @return:
     * @Author: sky
     * @date: 2024/11/17 20:56
     */
    @RequestMapping("getPermission")
    public Result<List<String>> getPermission(String userName) {
        try {
            log.info("PermissionController.getPermission.userName:{}", userName);
            Preconditions.checkArgument(!StringUtils.isBlank(userName), "用户openId不能为空");
            return Result.success(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("PermissionController.getPermission.error:{}", e.getMessage(), e);
            return Result.fail("查询用户权限信息失败");
        }
    }
}